package cc.ucanuup.web.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.ucanuup.common.support.base.Msg;
import cc.ucanuup.common.support.easyui.view.EasyuiPage;
import cc.ucanuup.service.base.entity.UpMenus;
import cc.ucanuup.service.base.entity.UpResource;
import cc.ucanuup.service.base.inter.UpMenuservice;
import cc.ucanuup.service.base.inter.UpResourceService;
import cc.ucanuup.web.base.model.ResourceModel;
import cc.ucanuup.web.base.model.SearchResourceModel;

/**
 * Description: TODO[描述该类概要功能介绍]
 *
 * @author guwenbin
 * @date 2018年2月3日
 * @version 1.0
 */
@Controller
@RequestMapping("/srm/resource")
public class UpResourceController {

	@Autowired
	private UpResourceService upResourceService;

	@Autowired
	private UpMenuservice upMenuservice;

	/**
	 * @Method: com.bozhon.web.base.controller.ResourceController.createOrUpdateResource
	 * @Description: 创建或更新资源
	 * @author: WenBin
	 * @date: 2017年10月13日
	 * @version: 1.0
	 * @param id
	 * @param request
	 * @return
	 * String
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value="/createOrUpdateResource")
	public String createOrUpdateResource(String id,HttpServletRequest request){
		UpResource res = upResourceService.findOne(id == null? "" : id);
		if(res == null){
			res = new UpResource();
		}
		List<UpMenus> menus = upMenuservice.findMenusByLevel(2);
		request.setAttribute("res", res);
		request.setAttribute("menuList", menus);
		return "base/power/menus/createOrUpdateResource";
	}

	/**
	 * @Method: com.bozhon.web.base.controller.ResourceController.saveOrUpdateResource
	 * @Description: 保存或者更新资源信息
	 * @author: WenBin
	 * @date: 2017年10月14日
	 * @version: 1.0
	 * @param resourceModel
	 * @param bindResult
	 * @param model
	 * @return
	 * @throws Exception
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value="/saveOrUpdateResource")
	@ResponseBody
	public Object saveOrUpdateResource(@Valid ResourceModel resourceModel,BindingResult bindResult, Model model) throws Exception{
		if (bindResult.hasErrors()) {
			List<ObjectError> map =bindResult.getAllErrors();
			return new Msg(false,"50",map);
		}
		UpResource res= upResourceService.findOne(resourceModel.getId() == null? "" :resourceModel.getId());
		if(res == null){
			res = new UpResource();
			BeanUtils.copyProperties(resourceModel,res);
			res.setDeleted(0);
			res.setStatus(0);
		}else{
			BeanUtils.copyProperties(resourceModel,res);
		}
		res = upResourceService.save(res);
		return new Msg();
	}
	/**
	 * @Method: com.bozhon.web.base.controller.ResourceController.queryResources
	 * @Description: 查询资源分页信息
	 * @author: WenBin
	 * @date: 2017年10月13日
	 * @version: 1.0
	 * @param searchResourceModel  查询资源信息
	 * @return
	 * @throws Exception
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value="/queryResources")
	@ResponseBody
	public Object queryResources(SearchResourceModel searchResourceModel) throws Exception{
		Page<UpResource> page =  upResourceService.queryResourcesAndSortItById(searchResourceModel);
		return new EasyuiPage<UpResource>(page.getContent(), page.getTotalElements(), page.getNumber()+1);
	}

	/**
	 * @Method: com.bozhon.web.base.controller.ResourceController.deleteResource
	 * @Description: 删除资源信息
	 * @author: WenBin
	 * @date: 2017年10月13日
	 * @version: 1.0
	 * @param id
	 * @return
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value="/deleteResource")
	@ResponseBody
	public Object deleteResource(String id){
		upResourceService.updateResourceDeleteStatus(id);
		return new Msg();
	}

	/**
	 * @Method: com.bozhon.web.base.controller.ResourceController.updateResourceStatus
	 * @Description: 更新资源状态
	 * @author: WenBin
	 * @date: 2017年10月13日
	 * @version: 1.0
	 * @param id
	 * @return
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value="/updateResourceStatus")
	@ResponseBody
	public Object updateResourceStatus(String id){
		upResourceService.updateResourceStatus(id);
		return new Msg();
	}
}
